# ----------------------------------------------------------------------
#    This is the
#
#    ██╗     ██╗ ██████╗  ██████╗  ██████╗ ██╗  ██╗████████╗███████╗
#    ██║     ██║██╔════╝ ██╔════╝ ██╔════╝ ██║  ██║╚══██╔══╝██╔════╝
#    ██║     ██║██║  ███╗██║  ███╗██║  ███╗███████║   ██║   ███████╗
#    ██║     ██║██║   ██║██║   ██║██║   ██║██╔══██║   ██║   ╚════██║
#    ███████╗██║╚██████╔╝╚██████╔╝╚██████╔╝██║  ██║   ██║   ███████║
#    ╚══════╝╚═╝ ╚═════╝  ╚═════╝  ╚═════╝ ╚═╝  ╚═╝   ╚═╝   ╚══════╝®
#
#    DEM simulation engine, released by
#    DCS Computing Gmbh, Linz, Austria
#    http://www.dcs-computing.com, office@dcs-computing.com
#
#    LIGGGHTS® is part of CFDEM®project:
#    http://www.liggghts.com | http://www.cfdem.com
#
#    Core developer and main author:
#    Christoph Kloss, christoph.kloss@dcs-computing.com
#
#    LIGGGHTS® is open-source, distributed under the terms of the GNU Public
#    License, version 2 or later. It is distributed in the hope that it will
#    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. You should have
#    received a copy of the GNU General Public License along with LIGGGHTS®.
#    If not, see http://www.gnu.org/licenses . See also top-level README
#    and LICENSE files.
#
#    LIGGGHTS® and CFDEM® are registered trade marks of DCS Computing GmbH,
#    the producer of the LIGGGHTS® software and the CFDEM®coupling software
#    See http://www.cfdem.com/terms-trademark-policy for details.
#
#-------------------------------------------------------------------------
#    Contributing author:
#    Tóth János (MATE, Gödöllő)
#-------------------------------------------------------------------------

PROJECT(LIGGGHTS C CXX)
CMAKE_MINIMUM_REQUIRED(VERSION 3.5.0)

SET(LIGGGHTS_MAJOR_VERSION 3)
SET(LIGGGHTS_MINOR_VERSION 8)
SET(LIGGGHTS_PATCH_VERSION 0)
SET(LIGGGHTS_VERSION ${LIGGGHTS_MAJOR_VERSION}.${LIGGGHTS_MINOR_VERSION}.${LIGGGHTS_PATCH_VERSION})

# NOTE: Implement these?
# https://www.cfdem.com/media/DEM/docu/Section_start.html#making-liggghts-r-public
# -DLAMMPS_MEMALIGN
# -DLAMMPS_XDR
# -DPACK_ARRAY
# -DPACK_POINTER
# -DPACK_MEMCPY

#=======================================
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cMake")
INCLUDE(Version)
INCLUDE(Style)
INCLUDE(Model)

#=======================================
SET(ENABLED_OPTIONS "")
SET(DISABLED_OPTIONS "")

SET(ENABLED_MODELS "")

OPTION(DISABLE_ALL "Disable all features/models (OFF by default)" OFF)
OPTION(ENABLE_ALL  "Enable all features/models (OFF by default)"  OFF)

SET(DEFAULT_ON ON CACHE INTERNAL "Default ON value")
SET(DEFAULT_OFF OFF CACHE INTERNAL "Default OFF value")

IF(DISABLE_ALL)
  SET(DEFAULT_ON OFF)
ENDIF()

IF(ENABLE_ALL)
  SET(DEFAULT_OFF ON)
ENDIF()

OPTION(ENABLE_MPI    "Use MPI"      ${DEFAULT_ON})
OPTION(ENABLE_VTK    "Use dump_vtk" ${DEFAULT_ON})
OPTION(ENABLE_JPEG   "Use libjpeg"  ${DEFAULT_OFF})
OPTION(ENABLE_PNG    "Use libpng"   ${DEFAULT_OFF})
OPTION(ENABLE_FFMPEG "Use ffmpeg"   ${DEFAULT_OFF})
OPTION(ENABLE_GZIP   "Use gzip"     ${DEFAULT_OFF})

OPTION(ENABLE_SQ  "Use Superquadrics" ${DEFAULT_OFF})

OPTION(ENABLE_SMALLBIG   "Use of 4-byte (small) vs 8-byte (big) integers."        ON)
OPTION(ENABLE_BIGBIG     "Huge molecular systems with more than 2 billion atoms." OFF)
OPTION(ENABLE_SMALLSMALL "Machine does not support 64-bit integers."              OFF)

OPTION(ENABLE_LONGLONG_TO_LONG "System does not recognize “long long” data types." OFF)

# 9 normal models
OPTION(ENABLE_MODEL_HERTZ "Hertz Model" ${DEFAULT_ON})
OPTION(ENABLE_MODEL_HOOKE "Hooke Model" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_HOOKE_HYSTERESIS "Hooke Hysteresis Model" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_HOOKE_STIFFNESS "Hooke Stiffness Model" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_EDINBURGH "Edinburgh Model" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_EDINBURGH_STIFFNESS "Edinburgh Stiffness Model" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_HERTZ_STIFFNESS "Hertz Stiffness Model" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_LUDING "Luding Model" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_THORNTON_NING "Thornton Ning Model" ${DEFAULT_OFF})

# 4 tangential models
OPTION(ENABLE_MODEL_TANGENTIAL_OFF "Tangential Model Off" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_TANGENTIAL_HISTORY "Tangential Model History" ${DEFAULT_ON})
OPTION(ENABLE_MODEL_TANGENTIAL_NO_HISTORY "Tangential Model No History" ${DEFAULT_ON})
OPTION(ENABLE_MODEL_TANGENTIAL_LUDING "Tangential Model Luding" ${DEFAULT_OFF})

# 5 cohesion models
OPTION(ENABLE_MODEL_COHESION_OFF "Cohesion Model Off" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_COHESION_SJKR "Cohesion Model SJKR" ${DEFAULT_ON})
OPTION(ENABLE_MODEL_COHESION_SJKR2 "Cohesion Model SJKR2" ${DEFAULT_ON})
OPTION(ENABLE_MODEL_COHESION_WASHINO_CAPILLARY_VISCOUS "Cohesion Model Washino/capillary/viscous" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_COHESION_EASO_CAPILLARY_VISCOUS "Cohesion Model Easo/capillary/viscous" ${DEFAULT_OFF})

# 6 rolling models
OPTION(ENABLE_MODEL_ROLLING_OFF "Rolling Model Off" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_ROLLING_CDT "Rolling Model CDT" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_ROLLING_EPSD "Rolling Model EPSD" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_ROLLING_EPSD2 "Rolling Model EPSD2" ${DEFAULT_ON})
OPTION(ENABLE_MODEL_ROLLING_EPSD3 "Rolling Model ESPD3" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_ROLLING_LUDING "Rolling Model Luding" ${DEFAULT_OFF})

# 3 surface models
OPTION(ENABLE_MODEL_SURFACE_DEFAULT "Surface Model Default" ${DEFAULT_ON})
OPTION(ENABLE_MODEL_SURFACE_SUPERQUADRIC "Surface Model Superquadric" ${DEFAULT_OFF})
OPTION(ENABLE_MODEL_SURFACE_MULTICONTACT "Surface Model Multicontact" ${DEFAULT_OFF})

#=======================================
IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  IF(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
    SET(CMAKE_CXX_STANDARD 14)
  ELSE()
    SET(CMAKE_CXX_STANDARD 17)
  ENDIF()
  SET(CMAKE_CXX_STANDARD_REQUIRED ON)  

  SET(OPT_CXX_FLAGS "")
  MESSAGE(STATUS "Compiler: clang")
ELSEIF(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"))
  IF(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
    SET(CMAKE_CXX_STANDARD 14)
  ELSE()
    SET(CMAKE_CXX_STANDARD 17)
  ENDIF()
  SET(CMAKE_CXX_STANDARD_REQUIRED ON)

  SET(OPT_CXX_FLAGS "-O2 -ffast-math -funroll-loops -fstrict-aliasing -Wall -Wextra -Wno-uninitialized -Wno-literal-suffix -fPIC")
  MESSAGE(STATUS "Compiler: gcc")
ELSEIF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
	SET(OPT_CXX_FLAGS "-O3 -funroll-loops -fstrict-aliasing -W -Wno-uninitialized -march=native -xHost")
  MESSAGE(STATUS "Compiler: intel")
ELSEIF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  SET(OPT_CXX_FLAGS "/O2 /bigobj")

  ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D_USE_MATH_DEFINES -DNOMINMAX)
  ADD_DEFINITIONS("/wd4101 /wd4244 /wd4267")

  MESSAGE(STATUS "Compiler: msvc")
ENDIF()

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPT_CXX_FLAGS}")

IF(WIN32)
  INCLUDE_DIRECTORIES(WINDOWS/extra)

  FIND_PACKAGE(Cygwin REQUIRED)

  FOREACH(flag_var
    CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
    CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    SET(${flag_var} "${${flag_var}} /MP4")
  ENDFOREACH()
ENDIF()

#=======================================
# based on: https://github.com/schrummy14/LIGGGHTS_Flexible_Fibers/blob/master/src/WINDOWS/CMake_patch.zip

SCAN_STYLES()
GENERATE_STYLES()

#=======================================
# based on: https://github.com/schrummy14/LIGGGHTS_Flexible_Fibers/blob/master/src/WINDOWS/CMake_patch.zip

IF(ENABLE_MODEL_HERTZ)
  ADD_CONTACT_MODEL("HERTZ" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} HERTZ")
ENDIF()
IF(ENABLE_MODEL_HOOKE)
  ADD_CONTACT_MODEL("HOOKE" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} HOOKE")
ENDIF()
IF(ENABLE_MODEL_HOOKE_HYSTERESIS)
  ADD_CONTACT_MODEL("HOOKE_HYSTERESIS" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} HOOKE_HYSTERESIS")
ENDIF()
IF(ENABLE_MODEL_HOOKE_STIFFNESS)
  ADD_CONTACT_MODEL("HOOKE_STIFFNESS" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} HOOKE_STIFFNESS")
ENDIF()
IF(ENABLE_MODEL_EDINBURGH)
  ADD_CONTACT_MODEL("EDINBURGH" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} EDINBURGH")
ENDIF()
IF(ENABLE_MODEL_EDINBURGH_STIFFNESS)
  ADD_CONTACT_MODEL("EDINBURGH_STIFFNESS" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} EDINBURGH_STIFFNESS")
ENDIF()
IF(ENABLE_MODEL_HERTZ_STIFFNESS)
  ADD_CONTACT_MODEL("HERTZ_STIFFNESS" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} HERTZ_STIFFNESS")
ENDIF()
IF(ENABLE_MODEL_LUDING)
  ADD_CONTACT_MODEL("LUDING" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} LUDING")
ENDIF()
IF(ENABLE_MODEL_THORNTON_NING)
  ADD_CONTACT_MODEL("THORNTON_NING" "" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} THORNTON_NING")
ENDIF()

# 4 tangential models
IF(ENABLE_MODEL_TANGENTIAL_OFF)
  ADD_CONTACT_MODEL("" "TANGENTIAL_OFF" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} TANGENTIAL_OFF")
ENDIF()
IF(ENABLE_MODEL_TANGENTIAL_HISTORY)
  ADD_CONTACT_MODEL("" "TANGENTIAL_HISTORY" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} TANGENTIAL_HISTORY")
ENDIF()
IF(ENABLE_MODEL_TANGENTIAL_NO_HISTORY)
  ADD_CONTACT_MODEL("" "TANGENTIAL_NO_HISTORY" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} TANGENTIAL_NO_HISTORY")
ENDIF()
IF(ENABLE_MODEL_TANGENTIAL_LUDING)
  ADD_CONTACT_MODEL("" "TANGENTIAL_LUDING" "" "" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} TANGENTIAL_LUDING")
ENDIF()

# 5 cohesion models
IF(ENABLE_MODEL_COHESION_OFF)
  ADD_CONTACT_MODEL("" "" "COHESION_OFF" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} COHESION_OFF")
ENDIF()
IF(ENABLE_MODEL_COHESION_SJKR)
  ADD_CONTACT_MODEL("" "" "COHESION_SJKR" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} COHESION_SJKR")
ENDIF()
IF(ENABLE_MODEL_COHESION_SJKR2)
  ADD_CONTACT_MODEL("" "" "COHESION_SJKR2" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} COHESION_SJKR2")
ENDIF()
IF(ENABLE_MODEL_COHESION_WASHINO_CAPILLARY_VISCOUS)
  ADD_CONTACT_MODEL("" "" "COHESION_WASHINO_CAPILLARY_VISCOUS" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} COHESION_WASHINO_CAPILLARY_VISCOUS")
ENDIF()
IF(ENABLE_MODEL_COHESION_EASO_CAPILLARY_VISCOUS)
  ADD_CONTACT_MODEL("" "" "COHESION_EASO_CAPILLARY_VISCOUS" "" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} COHESION_EASO_CAPILLARY_VISCOUS")
ENDIF()

# 6 rolling models
IF(ENABLE_MODEL_ROLLING_OFF)
  ADD_CONTACT_MODEL("" "" "" "ROLLING_OFF" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} ROLLING_OFF")
ENDIF()
IF(ENABLE_MODEL_ROLLING_CDT)
  ADD_CONTACT_MODEL("" "" "" "ROLLING_CDT" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} ROLLING_CDT")
ENDIF()
IF(ENABLE_MODEL_ROLLING_EPSD)
  ADD_CONTACT_MODEL("" "" "" "ROLLING_EPSD" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} ROLLING_EPSD")
ENDIF()
IF(ENABLE_MODEL_ROLLING_EPSD2)
  ADD_CONTACT_MODEL("" "" "" "ROLLING_EPSD2" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} ROLLING_EPSD2")
ENDIF()
IF(ENABLE_MODEL_ROLLING_EPSD3)
  ADD_CONTACT_MODEL("" "" "" "ROLLING_EPSD3" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} ROLLING_EPSD3")
ENDIF()
IF(ENABLE_MODEL_ROLLING_LUDING)
  ADD_CONTACT_MODEL("" "" "" "ROLLING_LUDING" "")
  SET(ENABLED_MODELS "${ENABLED_MODELS} ROLLING_LUDING")
ENDIF()

# 3 surface models
IF(ENABLE_MODEL_SURFACE_DEFAULT)
  ADD_CONTACT_MODEL("" "" "" "" "SURFACE_DEFAULT")
  SET(ENABLED_MODELS "${ENABLED_MODELS} SURFACE_DEFAULT")
ENDIF()
IF(ENABLE_MODEL_SURFACE_SUPERQUADRIC)
  ADD_CONTACT_MODEL("" "" "" "" "SURFACE_SUPERQUADRIC")
  SET(ENABLED_MODELS "${ENABLED_MODELS} SURFACE_SUPERQUADRIC")
ENDIF()
IF(ENABLE_MODEL_SURFACE_MULTICONTACT)
  ADD_CONTACT_MODEL("" "" "" "" "SURFACE_MULTICONTACT")
  SET(ENABLED_MODELS "${ENABLED_MODELS} SURFACE_MULTICONTACT")
ENDIF()

WRITE_WHITELIST()

#========================================
GET_SUBDIRS(dirList CMakeLists.txt)
FOREACH(dir ${dirList})
  ADD_SUBDIRECTORY(${dir})
ENDFOREACH()

#=======================================
FILE(GLOB SOURCES *.cpp)
FILE(GLOB HEADERS *.h)

ADD_LIBRARY(liggghts_obj OBJECT ${SOURCES})

ADD_LIBRARY(liggghts_static STATIC $<TARGET_OBJECTS:liggghts_obj>)
SET_TARGET_PROPERTIES(liggghts_static PROPERTIES OUTPUT_NAME liggghts)
SET_PROPERTY(TARGET liggghts_static PROPERTY PUBLIC_HEADER ${HEADERS})

ADD_LIBRARY(liggghts_shared SHARED $<TARGET_OBJECTS:liggghts_obj>)
SET_TARGET_PROPERTIES(liggghts_shared PROPERTIES OUTPUT_NAME liggghts)
SET_PROPERTY(TARGET liggghts_shared PROPERTY PUBLIC_HEADER ${HEADERS})

ADD_EXECUTABLE(liggghts_bin $<TARGET_OBJECTS:liggghts_obj>)
SET_TARGET_PROPERTIES(liggghts_bin PROPERTIES OUTPUT_NAME liggghts)

#=======================================
IF(ENABLE_MPI)
  FIND_PACKAGE(MPI)

  IF(MPI_FOUND)
    INCLUDE_DIRECTORIES(${MPI_INCLUDE_PATH})
    TARGET_LINK_LIBRARIES(liggghts_static PUBLIC ${MPI_LIBRARIES})
    TARGET_LINK_LIBRARIES(liggghts_shared PUBLIC ${MPI_LIBRARIES})
    TARGET_LINK_LIBRARIES(liggghts_bin PUBLIC ${MPI_LIBRARIES})

    IF(MPI_COMPILE_FLAGS)
      SET_TARGET_PROPERTIES(liggghts_static PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
      SET_TARGET_PROPERTIES(liggghts_shared PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
      SET_TARGET_PROPERTIES(liggghts_bin PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
    ENDIF()

    IF(MPI_LINK_FLAGS)
      SET_TARGET_PROPERTIES(liggghts_static PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
      SET_TARGET_PROPERTIES(liggghts_shared PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
      SET_TARGET_PROPERTIES(liggghts_bin PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
    ENDIF()

    SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} MPI")
  ELSE()
    MESSAGE(FATAL_ERROR "MPI NOT found!")
  ENDIF()
ELSE()
  SET(DISABLED_OPTIONS "${DISABLED_OPTIONS} MPI")
  MESSAGE(STATUS "Using MPI stubs")
ENDIF()

#=======================================
IF(ENABLE_VTK)
  FIND_PACKAGE(VTK COMPONENTS NO_MODULE)

  IF(VTK_FOUND)
    IF(VTK_VERSION VERSION_LESS "8.90.0")
      INCLUDE(${VTK_USE_FILE})
    ELSE()
      INCLUDE_DIRECTORIES("${VTK_PREFIX_PATH}/include/vtk-${VTK_VERSION_MAJOR}.${VTK_VERSION_MINOR}")
    ENDIF()
    
    TARGET_LINK_LIBRARIES(liggghts_static PUBLIC ${VTK_LIBRARIES})
    TARGET_LINK_LIBRARIES(liggghts_shared PUBLIC ${VTK_LIBRARIES})
    TARGET_LINK_LIBRARIES(liggghts_bin PUBLIC ${VTK_LIBRARIES})

    ADD_DEFINITIONS(-DLAMMPS_VTK)
    SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} VTK")

    MESSAGE(STATUS "Found VTK: version " ${VTK_VERSION})
  ELSE()
    MESSAGE(FATAL_ERROR "VTK NOT found!")
  ENDIF()
ELSE()
  SET(DISABLED_OPTIONS "${DISABED_OPTIONS} VTK")
ENDIF()

#=======================================
IF(ENABLE_JPEG)
  FIND_PACKAGE(JPEG)

  IF(JPEG_FOUND)
    INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})
    ADD_DEFINITIONS(-DLAMMPS_JPEG)
    TARGET_LINK_LIBRARIES(liggghts_static PUBLIC jpeg)
    TARGET_LINK_LIBRARIES(liggghts_shared PUBLIC jpeg)
    TARGET_LINK_LIBRARIES(liggghts_bin PUBLIC jpeg)
    SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} JPEG")
  ELSE()
    MESSAGE(FATAL_ERROR "JPEG NOT found!")
  ENDIF()
ELSE()
  SET(DISABLED_OPTIONS "${DISABLED_OPTIONS} JPEG")
ENDIF()

#=======================================
IF(ENABLE_PNG)
  FIND_PACKAGE(PNG)

  IF(PNG_FOUND)
    INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
    ADD_DEFINITIONS(-DLAMMPS_PNG)
    TARGET_LINK_LIBRARIES(liggghts_static PUBLIC ${PNG_LIBRARY})
    TARGET_LINK_LIBRARIES(liggghts_shared PUBLIC ${PNG_LIBRARY})
    TARGET_LINK_LIBRARIES(liggghts_bin PUBLIC ${PNG_LIBRARY})
    SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} PNG")
  ELSE()
    MESSAGE(FATAL_ERROR "PNG NOT found!")
  ENDIF()
ELSE()
  SET(DISABLED_OPTIONS "${DISABLED_OPTIONS} PNG")
ENDIF()

#=======================================
IF(ENABLE_FFMPEG)
  FIND_PATH(AVCODEC_INCLUDE_DIR libavcodec/avcodec.h)
  FIND_LIBRARY(AVCODEC_LIBRARY avcodec)

  FIND_PATH(AVFORMAT_INCLUDE_DIR libavformat/avformat.h)
  FIND_LIBRARY(AVFORMAT_LIBRARY avformat)

  FIND_PATH(AVUTIL_INCLUDE_DIR libavutil/avutil.h)
  FIND_LIBRARY(AVUTIL_LIBRARY avutil)

  IF((NOT AVCODEC_INCLUDE_DIR-NOTFOUND)
    AND (NOT AVFORMAT_INCLUDE_DIR-NOTFOUND)
    AND (NOT AVUTIL_INCLUDE_DIR-NOTFOUND)
    AND (NOT AVCODEC_LIBRARY-NOTFOUND)
    AND (NOT AVFORMAT_LIBRARY-NOTFOUND)
    AND (NOT AVUTIL_LIBRARY-NOTFOUND)
  )
    INCLUDE_DIRECTORIES(${AVCODEC_INCLUDE_DIR} ${AVFORMAT_INCLUDE_DIR} ${AVUTIL_INCLUDE_DIR})
    ADD_DEFINITIONS(-DLAMMPS_FFMPEG)
    TARGET_LINK_LIBRARIES(liggghts_static PUBLIC ${AVCODEC_LIBRARY} ${AVFORMAT_LIBRARY} ${AVUTIL_LIBRARY})
    TARGET_LINK_LIBRARIES(liggghts_shared PUBLIC ${AVCODEC_LIBRARY} ${AVFORMAT_LIBRARY} ${AVUTIL_LIBRARY})
    TARGET_LINK_LIBRARIES(liggghts_bin PUBLIC ${AVCODEC_LIBRARY} ${AVFORMAT_LIBRARY} ${AVUTIL_LIBRARY})
    
    SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} FFMPEG")
    
    MESSAGE(STATUS "FFMPEG found!")
  ELSE()
    MESSAGE(FATAL_ERROR "FFMPEG NOT found!")
  ENDIF()
ELSE()
  SET(DISABLED_OPTIONS "${DISABLED_OPTIONS} FFMPEG")
ENDIF()

#=======================================
IF(ENABLE_GZIP)
  FIND_PROGRAM(GZIP "gzip")

  IF(GZIP)
    ADD_DEFINITIONS(-DLAMMPS_GZIP)

    SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} GZIP")

    MESSAGE(STATUS "GZIP found!")
  ELSE()
    MESSAGE(FATAL_ERROR "GZIP NOT found!")
  ENDIF()
ELSE()
  SET(DISABLED_OPTIONS "${DISABLED_OPTIONS} GZIP")
ENDIF()

#=======================================
IF(ENABLE_SQ)
  FIND_PACKAGE(Boost)

  IF(Boost_FOUND)
    INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
    ADD_DEFINITIONS(-DSUPERQUADRIC_ACTIVE_FLAG -DNONSPHERICAL_ACTIVE_FLAG)
    TARGET_LINK_LIBRARIES(liggghts_static PUBLIC ${Boost_LIBRARY})
    TARGET_LINK_LIBRARIES(liggghts_shared PUBLIC ${Boost_LIBRARY})
    TARGET_LINK_LIBRARIES(liggghts_bin PUBLIC ${Boost_LIBRARY})

    SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} SQ")

    MESSAGE(STATUS "Using Superquadrics")
  ELSE()
    MESSAGE(FATAL_ERROR "Boost NOT found!")
  ENDIF()
ELSE()
  SET(DISABLED_OPTIONS "${DISABLED_OPTIONS} SQ")
ENDIF()

#=======================================
IF(ENABLE_SMALLBIG)
  IF(ENABLE_BIGBIG OR ENABLE_SMALLSMALL)
    MESSAGE(FATAL_ERROR "Use only one of these options: SMALLBIG, BIGBIG, SMALLSMALL")
  ENDIF()

  ADD_DEFINITIONS(-DLAMMPS_SMALLBIG)
  SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} SMALLBIG")
ELSEIF(ENABLE_BIGBIG)
  IF(ENABLE_SMALLBIG OR ENABLE_SMALLSMALL)
    MESSAGE(FATAL_ERROR "Use only one of these options: SMALLBIG, BIGBIG, SMALLSMALL")
  ENDIF()

  ADD_DEFINITIONS(-DLAMMPS_BIGBIG)
  SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} BIGBIG")
ELSEIF(ENABLE_SMALLSMALL)
  IF(ENABLE_BIGBIG OR ENABLE_SMALLBIG)
    MESSAGE(FATAL_ERROR "Use only one of these options: SMALLBIG, BIGBIG, SMALLSMALL")
  ENDIF()
  
  ADD_DEFINITIONS(-DLAMMPS_SMALLSMALL)
  SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} SMALLSMALL")
ELSE()
  ADD_DEFINITIONS(-DLAMMPS_SMALLBIG)
  SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} SMALLBIG")
ENDIF()

IF(ENABLE_LONGLONG_TO_LONG)
  ADD_DEFINITIONS(-DLAMMPS_LONGLONG_TO_LONG)
  SET(ENABLED_OPTIONS "${ENABLED_OPTIONS} LONGLONG_TO_LONG")
ENDIF()

#=======================================
GENERATE_VERSION_H()

#=======================================
MESSAGE(STATUS "=======================================")
MESSAGE(STATUS "liggghts ${LIGGGHTS_VERSION}")
MESSAGE(STATUS "Enabled options:${ENABLED_OPTIONS}")
MESSAGE(STATUS "Disabled options:${DISABLED_OPTIONS}")
MESSAGE(STATUS "Enabled models:${ENABLED_MODELS}")
MESSAGE(STATUS "=======================================")

#=======================================
INSTALL(TARGETS liggghts_bin liggghts_static liggghts_shared
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  PUBLIC_HEADER DESTINATION include)
IF(WIN32)
  INSTALL(DIRECTORY WINDOWS DESTINATION include)
ENDIF()
